﻿using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Quickuse.Logging;
using System;
using System.Collections.Generic;
using System.Text;

namespace Quickuse.ConsoleApp
{
    public class LogHelper
    {
        private static readonly ILoggerFactory loggerFactory;

        static LogHelper()
        {
            var configuration = new ConfigurationBuilder()
                .AddJsonFile("appsettings.json", false, true)
                .Build();

            loggerFactory = LoggerFactory.Create(builder =>
            {
                builder.AddConfiguration(configuration.GetSection("Logging"));
                builder.AddConsole();
                builder.AddFile(configuration);
                builder.AddEmail(configuration);
            });
        }

        public static ILogger CreateLogger<T>()
        {
            return loggerFactory.CreateLogger<T>();
        }

        /// <summary>
        /// 写入log
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="logmessage"></param>
        public static void LogError<T>(string logmessage)
        {
            var logger = loggerFactory.CreateLogger<T>();
            logger.LogError(logmessage);
        }
    }
}
